﻿/*
 * NetConnection.h
 *
 */
#ifndef __GDK_NETCONNECTION_H__
#define __GDK_NETCONNECTION_H__

#include <gdk/core/Unknown.h>
#include <gdk/core/Stream.h>
#include <gdk/core/Uuid.h>
#include <gdk/core/MemoryStream.h>
#include <gdk/core/EventDispenser.h>
#include <gdk/core/Packet.h>
#include <gdk/core/Message.h>
#include <gdk/net/Socket.h>


namespace gdk
{

	class GDK_NET_EXPORT NetConnection : virtual public Unknown
	{
		DECLARE_CLASS_HANDLE(NetConnection)

	private:
		Uuid				_clientId;
		PacketReader		_pkgReader;
		PacketWriter		_pkgWriter;
		EventDispenser		_eventDispenser; //事件处理者

		MemoryStream::Handle _writeCache;
		OutputStream		_writeCache_os;
		InputStream			_writeCache_is;

	protected:
		NetConnection(void);
		~NetConnection(void);

	public:
		virtual bool attach(IO *io);
		virtual void close(void);
		virtual Uuid getPeerSocketId(void) const { return UUID_NULL; }
		//------------------------------------------------------------------------------

	protected:
		int readPacket(InputStream &is);
		//------------------------------------------------------------------------------

	public:
		__INLINE__ EventDispenser& getEventDispenser(void) { return _eventDispenser; }
		//------------------------------------------------------------------------------

	private:
		void createWriteCache(void);
	public:
		__INLINE__ void  setClientId(const Uuid &clientId) { _clientId = clientId; }
		__INLINE__ const Uuid& getClientId(void) const { return _clientId; }
		bool  send(const Message *msg);
		//------------------------------------------------------------------------------
	};
	typedef NetConnection::Handle             NetConnectionPtr;
	typedef std::map<Uuid, NetConnectionPtr>  NetConnectionMap;


} //namespace gdk


#endif //__GDK_NETCONNECTION_H__
